package components.propertyCtrls
{
	import UIFree.controls.uif_checkbox;
	import UIFree.controls.uif_label;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import components.c_checkboxLabel;
	
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	public class c_checkboxList extends c_listCtrl {
		public static const WEIGHT		: Number 	= 206;
		private static const ITEMHEIGHT	: int 		= 24;
		
		public function c_checkboxList(parent:uif_component, aData:Object, y:Number, a_height:Number) {
			super(parent, aData, y, a_height);
		}
		
		override protected function createItem(area: uif_ecomponent, a_item: Object, i: int): uif_ecomponent {
			var rect: Rectangle = area.getInner();
			var item: c_checkboxLabel = new c_checkboxLabel(area, rect.x, rect.y + i * ITEMHEIGHT, rect.width, ITEMHEIGHT, list.style.itemClass);
			item.label.text = getCaption(a_item);
			item.checkbox.checked = a_item.checked;
			item.checkbox.addEvent(Event.CHANGE, doChangeChecked);
			return item;
		}
		
		protected function doChangeChecked(e: Event): void {
			if ((list.selectIndex > -1) && (e.target.parent._data == list.selectItem._data)) {
				updateCurrentChecked();
				dispatchEvent(new Event(Event.CHANGE, true));				
			}
		}
		
		protected function updateCurrentChecked(): void {
			if (list.selectIndex > -1)
				list.list[list.selectIndex].checked = (list.selectItem as c_checkboxLabel).checkbox.checked;
		}
		
		override protected function getValue():Object {
			return list.selectIndex;
		}
		
		override protected function doChange(e:Event):void {
			updateCurrentChecked();
			super.doChange(e);
		}
	}
}